/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tramite.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.cip.esymons.tramite.be.Documento;
import javax.persistence.Query;

/**
 *
 * @author Freddy Infantes
 */
@Stateless
public class DocumentoFacade implements DocumentoFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Documento documento) {
        em.persist(documento);
    }

    public void edit(Documento documento) {
        em.merge(documento);
    }

    public void remove(Documento documento) {
        em.remove(em.merge(documento));
    }

    public Documento find(Object id) {
        return em.find(Documento.class, id);
    }

    public List<Documento> findAll() {
        return em.createQuery("select object(o) from Documento as o").getResultList();
    }

    public Documento buscaDocumento(String numero){

        try {
            return (Documento) em.createQuery("select object(o) from Documento as o where o.docNumero=?1").setParameter(1, numero).getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
    
    public List<Documento> listarXTransaccion(Object id) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append(" select d");
            sb.append(" from DocumentoIncluido di inner Join di.fkOpeCodigo o inner Join o.fkTraCodigo t inner Join di.fkDocCodigo d ");
            sb.append(" where t.traCodigo=?1");

            q = em.createQuery(sb.toString());

            q.setParameter(1, id);

            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList<Documento>();
        }
    }

    public List<Documento> paginadorDocumento(int cantidad, int posicion,String desc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select td From Documento d ");


            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where td.tdoDesc like ?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, "%" + desc + "%");
            }
            q.setFirstResult(posicion);
            q.setMaxResults(cantidad);
            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public int contarPaginadorDocumento( String desc) {
       try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select COUNT(td) From Documento td ");

            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where td.tdoDesc =?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, desc);
            }
            return new Integer(q.getSingleResult().toString());
        } catch (Exception ex) {
            return 0;
        }
    }
}
